import { createRouter , createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'
// 引入大菠萝
import { useAdminStore } from '@/store/index'
// 引入进度条
import { loadingStart , loadingClose}  from '@/utils/mixin'
// 导入组件
import login from '@/views/login/index.vue'
import layout from '@/views/layout/index.vue'
import NotFound from '@/views/NotFound/index.vue'
import home from '@/views/home/home.vue'

// 创建vue-router实例
const router = createRouter({
    // 创建路由模式------history模式
    history : createWebHistory(),
    // 创建路由表
    routes : [
        {
            path : '/',
            redirect : '/layout',
        },
        {
            path : '/layout',
            component : layout,
            children : [
                {
                    path : '',
                    redirect : '/home'
                },
                {
                    path : '/home',
                    name : 'home',
                    component : home,
                    meta : {title : '首页'}
                },
                {
                    path : '/goods/list',
                    component : () => import('@/views/Commodity/goods/goodList.vue'),
                    meta : {title : '商品管理'}
                },
                {
                    path : '/category/list',
                    component : () => import('@/views/Commodity/categoryList/categoryList.vue'),
                    meta : {title : '分类管理'}
                },
                {
                    path : '/skus/list',
                    component : () => import('@/views/Commodity/skus/Specifications.vue'),
                    meta : {title : '规格管理'}
                },
                {
                    path : '/coupon/list',
                    component : () => import('@/views/Commodity/coupon/index.vue'),
                    meta : {title : '优惠劵管理'}
                },
                {
                    path : '/image/list',
                    component : () => import('@/views/other/gallery/index.vue'),
                    meta : {title : "图库管理"}
                },
                {
                    path : '/notice/list',
                    component : () => import('@/views/other/notice/index.vue'),
                    meta : {title : "公告管理"}
                },
                {
                    path : '/manager/list',
                    component : () => import('@/views/admin/adminList/index.vue'),
                    meta : {title : '管理员列表'}
                },
                {
                    path : '/access/list',
                    component : () => import('@/views/admin/jurisdiction/index.vue'),
                    meta : {title : '权限管理'}
                },
                {
                    path : '/role/list',
                    component : () => import('@/views/admin/role/index.vue'),
                    meta : {title : '角色管理'}
                },
                {
                    path : '/distribution/index',
                    component : () => import('@/views/Distribution/distributionList/index.vue'),
                    meta : {title : '分销员管理'}
                },
                {
                    path : '/user/list',
                    component : () => import('@/views/user/userManage/index.vue'),
                    meta : {title:'用户管理'}
                },
                {
                    path : '/level/list',
                    component : () => import('@/views/user/Membershipl/index.vue'),
                    meta : {title:'会员等级'}
                }
            ]
        },
        {
            path : '/login',
            name : 'login',
            component : login,
            meta : {title : '登录页'}
        },
        {
            path : '/404',
            component : NotFound,
            meta : {title : '404页面找不到了'}
        },
        {
            path : '/:catchAll(.*)',
            redirect : '/404'
        }
    ]
})
// 路由全局守卫
router.beforeEach((to,from) => {
    // 切换路由，开启loading
    loadingStart()

    // const adminStore = useAdminStore()
    // adminStore.getAdminInfo(adminStore.adminInfo as object)
    // 页面标题
    document.title = to.meta.title === '404页面找不到了' ? to.meta.title : to.meta.title + ' | ' + ' 洋洋后台管理'
    // 判断是否有Token
    const token = window.localStorage.getItem('token')
    if(to.path === '/home'){
        if(!token && to.name !== 'login'){
            return { name : 'login'}            
        }
    }
    // 判断token是否过期
    if(to.path !== '/login'){
        const newDate :number = new Date().getTime()
        const oldDate :number = Number(window.localStorage.getItem('date'))
        if(newDate - oldDate > 7200000){
            ElMessage({
                message: '登录超时,请重新登录!',
                type: 'warning',
                duration : 2000
            })
            window.localStorage.removeItem('token')
            return { name : 'login'}
        }
    }
    if(token && to.path === '/login'){
        ElMessage({
            message: '已登录,切勿重复登录!',
            type: 'warning',
            duration : 2000
        })
        return { path : from.path ? from.path : '/'}
    }
})
// 路由后置守卫
router.afterEach((to, from) => {
    // 关闭loading
    loadingClose()
})

// 导出路由
export default router